﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Net;
using Intorise.Common;

namespace Model
{
    /// <summary>
    /// 服务器返回的消息
    /// </summary>
    public class Message
    {
        public MessageTypeEnum MessageType { get; set; }

        /// <summary>
        /// 数据长度
        /// </summary>
        public int Length { get; set; }
        /// <summary>
        /// 消息数据
        /// </summary>
        public byte[] Data { get; set; }
        /// <summary>
        /// 32位整数消息编码表示消息的用途
        /// </summary>
        public int Type { get; set; }
    }

    public class CurrentMapMsg : Message
    {
        public ushort ID { get; set; }
        public CurrentMapMsg(byte[] Data)
        {
            MessageType = MessageTypeEnum.CurrentMap;
            Type = 0x018706;
            this.Data = Data;
            ID = (ushort)IPAddress.NetworkToHostOrder(BitConverter.ToInt16(Data, 0));
        }
    }

    /// <summary>
    /// 进入地图
    /// </summary>
    public class MapEnteredMsg : Message
    {
        public MapEnteredMsg(byte[] Data)
        {
            MessageType = MessageTypeEnum.MapEntered;
            Type = 0x00018B00;
            this.Data = Data;
            Line = Data[0] / 2;
            X = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(Data, 1));
            Y = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(Data, 3));
            Location = new Point(X / 25, Y / 25);
        }
        public short X { get; private set; }
        public short Y { get; private set; }
        public Point Location { get; private set; }

        public int Line { get; private set; }
    }

    /// <summary>
    /// 位置变化
    /// </summary>
    public class LocationChangedMsg : Message
    {
        public LocationChangedMsg(byte[] Data)
        {
            MessageType = MessageTypeEnum.LocationChanged;
            Type = 0x00018B03;
            this.Data = Data;
            UserID = DataPackager.GetHostData64(Data, 0);
            X = DataPackager.GetHostData16(Data, 10);
            Y = DataPackager.GetHostData16(Data, 12);
            Location = new Point(X / 25, Y / 25);
        }
        public short X { get; private set; }
        public short Y { get; private set; }
        public long UserID { get; private set; }
        public Point Location { get; private set; }
    }

    /// <summary>
    /// 登陆消息
    /// </summary>
    public class LoginSuccessMsg : Message
    {
        public short Flag { get; private set; }
        /// <summary>
        /// 玩家编号
        /// </summary>
        public long ID { get; private set; }
        public string Name { get; private set; }
        public int Level { get; private set; }
        public byte[] Version { get; private set; }

        public LoginSuccessMsg(byte[] Data)
        {
            MessageType = MessageTypeEnum.LoginSuccess;
            Type = 0x00018705;
            if (Data[2] == 0)
            {
                ID = 0;
                return;
            }
            Flag = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(Data, 0));
            ID = IPAddress.NetworkToHostOrder(BitConverter.ToInt64(Data, 2));
            Version = Data.Skip(10).Take(2).ToArray();
            int length = DataPackager.GetHostData16(Data, 12);
            Name = Tools.GetString(Data, 14, length);
            //Level = DataPackager.GetHostData32(Data, 14 + length);
            var A1 = Data[14 + length];
            var A2 = Data[15 + length];
            if (A2 == 0)
                Level = A1 / 2;
            else
                Level = (A1 - 128) / 2 + A2 * 64;

            this.Data = Data;
        }
    }

    
     public class ProcessViewLevelMsg : Message
    {
        public short Flag { get; private set; }
        /// <summary>
        /// 玩家编号
        /// </summary>
        public long ID { get; private set; }
        public string Name { get; private set; }
        public int Level { get; private set; }
        public byte[] Version { get; private set; }

        public ProcessViewLevelMsg(byte[] Data)
        {
            int length = DataPackager.GetHostData16(Data, 10);
            int A1 = Data[16 + length];
            int A2 = Data[17 + length];
            if (Data[15 + length] == 0)
            {
                Level = A1/ 2;
            }
            else
                Level = A1 - 128 + A2 * 64;

        }
     }


    public class LoginSuccessMsg1 : Message
    {
        public short Flag { get; private set; }
        /// <summary>
        /// 玩家编号
        /// </summary>
        public long ID { get; private set; }
        public string Name { get; private set; }
        public int Level { get; private set; }
        public byte[] Version { get; private set; }

        public LoginSuccessMsg1(byte[] Data)
        {
            MessageType = MessageTypeEnum.LoginSuccess1;
            Type = 0x000187DA;
            
          //  Flag = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(Data, 0));
            ID = IPAddress.NetworkToHostOrder(BitConverter.ToInt64(Data, 27));
            Version = Data.Skip(35).Take(2).ToArray();
            int length = DataPackager.GetHostData16(Data, 40);
            Name = Tools.GetString(Data, 42, length);
            //Level = DataPackager.GetHostData32(Data, 14 + length);
            

            this.Data = Data;
        }
    }



    /// <summary>
    /// 传送返回消息
    /// </summary>
    public class TransmitMsg : Message
    {
        public Map CurrentMap { get; private set; }
        public TransmitMsg(byte[] Data)
        {
            MessageType = MessageTypeEnum.TransmitMap;
            Type = 0x00018AFD;
            this.Data = Data;
            CurrentMap = Maps.GetMap((ushort)IPAddress.NetworkToHostOrder(BitConverter.ToInt16(Data, 1)));
        }
    }

    public class MonsterRefreshedMsg : Message
    {
        public Point Location { get; set; }
        public string Name { get; set; }
        public Map CurrentMap { get; set; }
        public long ID { get; set; }

        public MonsterRefreshedMsg(byte[] Data)
        {
            MessageType = MessageTypeEnum.MonsterRefreshed;
            Type = 0x0001BDBD;
            this.Data = Data;
            try
            {
                ID = IPAddress.NetworkToHostOrder(BitConverter.ToInt64(Data, 0));
                short nameLength = 0;
                int x, y;

                nameLength = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(Data, 14));
                Name = Tools.GetString(Data, 16, nameLength);
                CurrentMap = Maps.GetMap((ushort)IPAddress.NetworkToHostOrder(BitConverter.ToInt16(Data, 25 + nameLength)));
                x = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(Data, 28 + nameLength)) / 25; //27
                y = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(Data, 30 + nameLength)) / 25; //29


                Location = new Point(x, y);
            }
            catch
            {
            }
        }
    }

    public class MonsterFoundedMsg : Message
    {
        public Point Location { get; set; }
        public string Name { get; set; }
        public Map CurrentMap { get; set; }
        public long ID { get; set; }

        public MonsterFoundedMsg(byte[] Data)
        {
            MessageType = MessageTypeEnum.MonsterFounded;
            Type = 0x00018b05;
            this.Data = Data;
            if (Data.Length > 100)
                return;
            ID = DataPackager.GetHostData64(Data, 6);
            short nameLength = DataPackager.GetHostData16(Data, 18);//20
            if (nameLength > 0)
            {
                Name = Tools.GetString(Data, 20, nameLength);
                CurrentMap = Maps.GetMap((ushort)DataPackager.GetHostData32(Data, 31 + nameLength));
            }
            int x = DataPackager.GetHostData16(Data, 35 + nameLength) / 25;//37
            int y = DataPackager.GetHostData16(Data, 37+ nameLength) / 25;//39
            Location = new Point(x, y);
        }
    }

    public class ResultMsg : Message
    {
        public string DataType { get; set; }
        public string ResultText { get; set; }
        public ResultMsg(byte[] data)
        {
            MessageType = MessageTypeEnum.ServerResult;
            Data = data;
            //int length = DataPackager.GetHostData16(data, 0);
            //DataType = Tools.GetString(data, 2, length);
            //length = 2 + DataType.Length + 2;
            //ResultText = Tools.ConvertChar(Tools.GetString(data, length, data.Length - length));
            ResultText = Tools.ConvertChar(Tools.GetString(data));
        }
    }

    public class UnderAttackMsg : Message//怪物攻击玩家的消息
    {
        /// <summary>
        /// 攻击者编号
        /// </summary>
        public long AttackerID { get; set; }
        /// <summary>
        /// 攻击目标编号
        /// </summary>
        public long TargetID { get; set; }
        /// <summary>
        /// 伤害
        /// </summary>
        public int Damage { get; set; }
        /// <summary>
        /// 玩家当前血量
        /// </summary>
        public int CurrentHP { get; set; }

        public UnderAttackMsg(byte[] Data)
        {
            //MessageType = MessageTypeEnum.Attack;
            //this.Data = Data;
            //TargetID = DataPackager.GetHostData64(Data, 0);
            //AttackerID = DataPackager.GetHostData64(Data, 10);
            ////byte A1 = Data[25];
            ////byte A2 = Data[26];
            ////Damage = (A1 - 128) / 2 + (A2 * 64);
            //byte A1 = Data[24];     //data长度30
            //Damage = A1 / 2;
            //A1 = Data[25];
            //byte A2 = Data[26];

            //CurrentHP = (A1 - 128) / 2 + (A2 * 64);
            MessageType = MessageTypeEnum.Attack;
            this.Data = Data;
            TargetID = DataPackager.GetHostData64(Data, 0);
            AttackerID = DataPackager.GetHostData64(Data, 10);
            //Damage = DataPackager.GetHostData32(Data, 21);
            //MonsterCurrentHP = DataPackager.GetHostData32(Data, 25);

            int A1 = Data[24];
            int A2 = Data[25];
            if (Data.Length == 28)
            {
                A1 = Data[Data.Length - 4];
                A2 = Data[Data.Length - 3];
                Damage = A1 / 2;
                CurrentHP = A2 / 2;

            }
            else
            {
                A1 = Data[Data.Length - 5];
                Damage = A1 / 2;
                //A1 = Data[25];
                //A2 = Data[26];
                A1 = Data[Data.Length - 4];
                A2 = Data[Data.Length - 3];
                if (A2 == 0)
                {
                    CurrentHP = A1 / 2;
                }
                else
                    CurrentHP = (A1 - 128) / 2 + (A2 * 64);
            }
                
        
        }
    }

    public class AttackMsg : Message//玩家攻击怪物的消息
    {
        /// <summary>
        /// 攻击者编号
        /// </summary>
        public long AttackerID { get; set; }
        /// <summary>
        /// 攻击目标编号
        /// </summary>
        public long TargetID { get; set; }
        /// <summary>
        /// 伤害
        /// </summary>
        public int Damage { get; set; }
        /// <summary>
        /// 怪物当前血量
        /// </summary>
        public int CurrentHP { get; set; }

        public AttackMsg(byte[] Data)
        {
            MessageType = MessageTypeEnum.Attack;
            this.Data = Data;
            TargetID = DataPackager.GetHostData64(Data, 0);
            AttackerID = DataPackager.GetHostData64(Data, 10);
            //Damage = DataPackager.GetHostData32(Data, 21);
            //MonsterCurrentHP = DataPackager.GetHostData32(Data, 25);
            int A1 = Data[24];
            int A2 = Data[25];
            if (Data[23] == 1)
            {
                A1 = Data[25];
                A2 = Data[26];
                Damage = 0;
                CurrentHP = (A1 - 128) / 2 + (A2 * 64);
            }
            else if (Data[24] == 2)
            {
                Damage = 0;
                A1 = Data[25];
                A2 = Data[26];
                CurrentHP = (A1 - 128) / 2 + (A2 * 64);
            }
            else
            {
                Damage = (A1 - 128) / 2 + (A2 * 64);
                A1 = Data[26];
                A2 = Data[27];
                //A1 = Data[Data.Length - 4]; //data=29
                //A2 = Data[Data.Length - 3];
                if (A1 == 0 && A2 == 0)
                {
                    CurrentHP = 0;
                }
                else if (A2 == 0)
                {
                    CurrentHP = A1 / 2;
                }
                else
                    CurrentHP = (A1 - 128) / 2 + (A2 * 64);
            }
            if (Damage > 5000)
            {
                ;
            }
        }
    }

    public class MonsterMovedMsg : Message
    {
        public long MoverID { get; private set; }
        public Point Location { get; private set; }

        public MonsterMovedMsg(byte[] Data)
        {
            MessageType = MessageTypeEnum.MonsterMoved;
            this.Data = Data;
            MoverID = DataPackager.GetHostData64(Data, 0);
            int x = DataPackager.GetHostData16(Data, 10) / 25;//8
            int y = DataPackager.GetHostData16(Data, 12) / 25;//10
            Location = new Point(x, y);
        }
    }

    public class PlayerMovedMsg : Message
    {
        public long MoverID { get; private set; }
        public Point Location { get; private set; }
        public byte[] MoveData { get; private set; }

        public PlayerMovedMsg(byte[] Data)
        {
            MessageType = MessageTypeEnum.PlayerMoved;
            this.Data = Data;
            MoverID = DataPackager.GetHostData64(Data, 0);
            int x = DataPackager.GetHostData16(Data, 10) / 25;
            int y = DataPackager.GetHostData16(Data, 12) / 25;
            Location = new Point(x, y);
            MoveData = Data.Skip(14).ToArray();
        }
    }

    public class HeathPointChangedMsg : Message
    {
        public long OwnerID { get; private set; }
        public int CurrentHP { get; private set; }
        public int TotalHP { get; private set; }

        public HeathPointChangedMsg(byte[] Data)
        {
            MessageType = MessageTypeEnum.HeathPointChanged;
            this.Data = Data;
            OwnerID = DataPackager.GetHostData64(Data, 0);
            byte A1, A2;
            A1 = Data[Data.Length - 4];
            A2 = Data[Data.Length - 3];
            CurrentHP = (A1 - 80) / 2 + (A2 * 64);

            A1 = Data[Data.Length - 2];
            A2 = Data[Data.Length - 1];
            TotalHP = (A1 - 80) / 2 + (A2 * 64);
            //CurrentHP = DataPackager.GetHostData32(Data, 8);
            //TotalHP = DataPackager.GetHostData32(Data, 12);
        }
    }

    public class PlayerFoundedMsg : Message
    {
        public long ID { get; private set; }
        public string Name { get; private set; }
        public int Level { get; private set; }
        public int MapID { get; private set; }
        public Point Location { get; private set; }
        public int CurrentHP { get; private set; }
        public int TotalHP { get; private set; }
        public int CurrentMP { get; private set; }
        public int TotalMP { get; private set; }

        public PlayerFoundedMsg(byte[] Data)
        {
            MessageType = MessageTypeEnum.PlayerFounded;
            this.Data = Data;
            ID = DataPackager.GetHostData64(Data, 18);
            int length = DataPackager.GetHostData32(Data, 28);
            Name = Tools.GetString(Data, 32, length);
            Level = DataPackager.GetHostData32(Data, 32 + length);
            int x = DataPackager.GetHostData16(Data, 36 + length);
            int y = DataPackager.GetHostData16(Data, 38 + length);
            Location = new Point(x, y);
            CurrentHP = DataPackager.GetHostData32(Data, 40 + length);
            TotalHP = DataPackager.GetHostData32(Data, 44 + length);
            CurrentMP = DataPackager.GetHostData32(Data, 48 + length);
            TotalMP = DataPackager.GetHostData32(Data, 52 + length);
        }
    }

    public class MpChangedMsg : Message
    {
        public long ID { get; private set; }
        public int CurrentMP { get; private set; }

        public MpChangedMsg(byte[] Data)
        {
            MessageType = MessageTypeEnum.MpChanged;
            this.Data = Data;
            ID = DataPackager.GetHostData64(Data, 0);
            byte a, b;
            a = Data[10];
            if (Data.Length == 12)
            {
                b = Data[11];
                CurrentMP = (a - 128) / 2 + (b * 64);
            }
            else
                CurrentMP = a / 2;
            
            
        }
    }

    public class HpChangedMsg : Message
    {
        public long ID { get; private set; }
        public int CurrentHP { get; private set; }

        public HpChangedMsg(byte[] Data)
        {
            MessageType = MessageTypeEnum.HpChanged;
            this.Data = Data;
            ID = DataPackager.GetHostData64(Data, 0);
            byte A1, A2, A3;
            if (Data.Length == 11)
            {
                A1 = Data[Data.Length - 1];
                CurrentHP = A1 / 2;
            }
            else if (Data[8] == 0x1c)
            {
                A1 = Data[Data.Length - 3];
                A2 = Data[Data.Length - 2];
                A3 = Data[Data.Length - 1];
                CurrentHP = (A1 - 128) / 2 + (A2 + (A3 - 1) * 128) * 64;
            }
            else
            {
                A1 = Data[Data.Length - 2];
                A2 = Data[Data.Length - 1];
                CurrentHP = (A1 - 128) / 2 + (A2 * 64);
            }
           
            //CurrentHP = (ushort)DataPackager.GetHostData16(Data, 10) / 4;
        }
    }

    public class GoldChangedMsg : Message
    {
        public int GoldCount { get; private set; }
        public byte[] GoldID { get; private set; }
        public GoldChangedMsg(byte[] Data)
        {
            MessageType = MessageTypeEnum.GoldChanged;
            this.Data = Data;
            byte A1 = Data[0];
            byte A2 = 1;
            if (Data.Length > 1)
                A2 = Data[1];
            byte A3 = 1;
            if (Data.Length >2)
                A3 = Data[2];
            byte A4 = 1;
            if (Data.Length == 4)
                A4 = Data[3];

            GoldID = Data.Skip(0).Take(Data.Length).ToArray();
            GoldCount = (A1 - 128) / 2 + (A2 + (A3 - 1 + (A4 - 1) * 128) * 128) * 64;
        }
    }

    /// <summary>
    /// 获得物品
    /// </summary>
    public class ItemGetedMsg : Message
    {
        public Thing Item { get; private set; }
        public long InstanceID { get; private set; }

        public ItemGetedMsg(byte[] Data)
        {
            MessageType = MessageTypeEnum.ItemGeted;
            this.Data = Data;
            InstanceID = DataPackager.GetHostData64(Data, 0);
            var ids = Data.Skip(10).Take(3).ToList();
            ids.Insert(0, 0);
            //ids.Add(0);
            uint id = (uint)DataPackager.GetHostData32(ids.ToArray(), 0);
            Item = Thing.GetThing(id);
            if (Item == null)
            {
                Item = new Thing { ID = (uint)id, Name = "未知物品", InstanceID = InstanceID };
            }
            else
            {
                Item.InstanceID = InstanceID;
            }
        }
    }

    public class ItemDropedMsg : Message
    {
        public int Count { get; private set; }
        public Thing DropedItem { get; private set; }
        /// <summary>
        /// 所属玩家编号
        /// </summary>
        public long OwnerID { get; private set; }
        public uint TypeID { get; private set; }
        public short X { get; private set; }
        public short Y { get; private set; }
        public string Name { get; private set; }

        public ItemDropedMsg(byte[] Data)
        {
            MessageType = MessageTypeEnum.ItemDroped;
            this.Data = Data;

            var typeDatas = Data.Skip(10).Take(3).ToList();
            typeDatas.Insert(0, 0);
            TypeID = (uint)DataPackager.GetHostData32(typeDatas.ToArray(), 0);
            //int length = DataPackager.GetHostData16(Data, 18);
           // Name = Tools.GetString(typeDatas.ToArray(), 0);
            DropedItem = Thing.GetThing(TypeID);
            if (DropedItem == null)
            {
                DropedItem = new Thing { Name = "未知物品", ID = (uint)TypeID };
                //if (TypeID > 1000000)//分类编号大于100万为卓越物品
                //{
                //    DropedItem.Name = "卓越装备";
                //}
            }
            DropedItem.InstanceID = DataPackager.GetHostData64(Data, 0);
            Count = DataPackager.GetHostData32(Data, 15);
            OwnerID = DataPackager.GetHostData64(Data, Data.Length - 26);
            short length = DataPackager.GetHostData16(Data, 16);
            X = DataPackager.GetHostData16(Data, Data.Length - 6);
            Y = DataPackager.GetHostData16(Data, Data.Length - 4);
        }
    }

    public class RideMsg : Message
    {
        public bool IsRide { get; private set; }
        public long PlayerID { get; private set; }

        public RideMsg(byte[] Data)
        {
            MessageType = MessageTypeEnum.Ride;
            this.Data = Data;
            PlayerID = DataPackager.GetHostData64(Data, 0);
            IsRide = BitConverter.ToBoolean(Data, 12);
        }
    }

    /// <summary>
    /// 捡到物品
    /// </summary>
    public class ItemPickupedMsg : Message
    {
        public uint TypeID { get; private set; }
        public long InstanceID { get; private set; }
        public Thing GetedItem { get; private set; }
        public ushort thingid { get; private set; }
        public int thingids { get; private set; }

        public ItemPickupedMsg(byte[] Data)
        {
            //00 01 97 14 84 D9 E5 BC B6 D3 AA F9 A0 01 86 BA 55 00 00 BA 03 
            MessageType = MessageTypeEnum.ItemPickuped;
            this.Data = Data;
            var typeDatas = Data.Skip(10).Take(3).ToList();
            var thingID = Data[10];
           // thingid = DataPackager.GetHostData64(Data, 11);
            typeDatas.Insert(0, 0);
            TypeID = (uint)DataPackager.GetHostData32(typeDatas.ToArray(), 0);

            thingid = (ushort)DataPackager.GetHostData16(Data, 11);
            thingids = Data[10];

            InstanceID = DataPackager.GetHostData64(Data, 0);
            GetedItem = Thing.GetThing(TypeID);
            if (GetedItem == null)
            {
                if (thingid == 42415 || thingid == 63150 || thingid == 34479 || thingid == 46511 || thingid == 50351 || thingid == 44450 || thingid == 47004 || thingid == 36777 || thingid == 65448 || thingid == 44713 || thingid == 48809 || thingid == 52649 || thingid == 46748)
                    GetedItem = new Thing { Name = "卓越弓手装备", ID = (uint)TypeID };
                else

                    GetedItem = new Thing { Name = "未知物品", ID = (uint)TypeID };
                //if (TypeID > 1000000)//分类编号大于100万为卓越装备
                //{
                //    GetedItem.Name = "卓越装备";
                //}
            }
            GetedItem.InstanceID = InstanceID;
        }
    }

    public class PickupItemMsg : Message
    {
        public string Name { get; private set; }
        public uint TypeID { get; private set; }

        public PickupItemMsg(byte[] Data)
        {
            this.Data = Data;
            int length = DataPackager.GetHostData16(Data, 18);
            Name = Tools.GetString(Data, 20, length);
            TypeID = (uint)DataPackager.GetHostData32(Data.Skip(19).Take(length).ToList().ToArray(), 0);
        }
    }

    public class SignCountMsg : Message
    {
        public int Count { get; set; }
        public SignCountMsg(byte[] data)
        {
            //     Count = DataPackager.GetHostData16(data, 0);
        }
    }


    public class SellEquipmentMsg : Message
    {
        public long InstanceID { get; private set; }

        public SellEquipmentMsg(byte[] Data)
        {
            InstanceID = DataPackager.GetHostData64(Data, 0);
        }
           
    }

    public class CleanBackpackMsg : Message
    {
        public long []InstanceID { get; private set; }
        public uint []TypeID { get; private set; }
        public Thing []GetedItem { get; private set; }
        int i;
        public int ThingNum { get; private set; }
        public byte[] Versions { get; private set; }
        public CleanBackpackMsg(byte[] Data,Player player)
        {
            var typeDatas = Data.Skip(10).Take(3).ToList();
            ThingNum = 0;

            InstanceID = new long[100];
            TypeID = new uint[100];
            GetedItem = new Thing[100];
            for (i = 0; i < Data.Length - 10; i++)
            {
                Versions = Data.Skip(i).Take(2).ToArray();
                if (Versions[0] == player.Version[0] && Versions[1] == player.Version[1])
                
                {
                    
                    InstanceID[ThingNum] = DataPackager.GetHostData64(Data, i - 8);
                    typeDatas = Data.Skip(i + 2).Take(3).ToList();
                    typeDatas.Insert(0, 0);
                    TypeID[ThingNum] = (uint)DataPackager.GetHostData32(typeDatas.ToArray(), 0);
                    GetedItem[ThingNum] = Thing.GetThing(TypeID[ThingNum]);
                    if (GetedItem[ThingNum] == null)
                    {
                        GetedItem[ThingNum] = new Thing { Name = "未知物品", ID = (uint)TypeID[ThingNum] };
                        
                    }
                    GetedItem[ThingNum].InstanceID = InstanceID[ThingNum];
                    ThingNum++;
                }
                else if (Versions[0] == 122)
                {
                    InstanceID[ThingNum] = DataPackager.GetHostData64(Data, i - 8);
                    typeDatas = Data.Skip(i + 1).Take(3).ToList();
                    typeDatas.Insert(0, 0);
                    TypeID[ThingNum] = (uint)DataPackager.GetHostData32(typeDatas.ToArray(), 0);
                    GetedItem[ThingNum] = Thing.GetThing(TypeID[ThingNum]);
                    GetedItem[ThingNum].InstanceID = InstanceID[ThingNum];
                    ThingNum++;
                }
            }
        }

    }

    public class LevelUpMsg : Message
    {
        public int Level { get; set; }
        public long PlayerID { get; private set; }
        public LevelUpMsg(byte[] data)
        {
            PlayerID = DataPackager.GetHostData64(data, 0);
            //var A1 = data[data.Length - 2];
            //var A2 = data[data.Length - 1];
            var A1 = data[10];
            var A2 = data[11];
            if (A2 == 0)
            {
                Level = A1 / 2;
            }
            else
            {
                A2 = data[11];
                Level = (A1 - 128) / 2 + (A2 * 64);
            }
        }
    }
    /// <summary>
    /// 交易锁定
    /// </summary>
    public class LockTradeMsg : Message
    {
        /// <summary>
        /// 锁定交易的玩家编号
        /// </summary>
        public long InvitePlayerID { get; private set; }
        public ushort InviteTrue { get; private set; }


        public LockTradeMsg(byte[] data)
        {
            MessageType = MessageTypeEnum.LockTrade;
            this.Data = data;
            InvitePlayerID = DataPackager.GetHostData64(Data, 0);
            InviteTrue = data[data.Length - 1];
        }
    }

    /// <summary>
    /// 同意交易
    /// </summary>
    public class AgreeTradeMsg : Message
    {

        public AgreeTradeMsg(byte[] data)
        {
            MessageType = MessageTypeEnum.AgreeTrade;
            this.Data = data;
        }
    }

    /// <summary>
    /// 交易锁定
    /// </summary>
    public class ProcessMailDataMsg : Message
    {
        /// <summary>
        /// 奖励编号
        /// </summary>
        public long ThingID { get; private set; }
        public int ThingNum { get; private set; }

        public ProcessMailDataMsg(byte[] data)
        {
            MessageType = MessageTypeEnum.MailData;
            this.Data = data;
            // ThingID[] = DataPackager.GetHostData64(Data, 0);
            ThingNum = 0;
            ThingID = 0;
            for (int i = 9; i < Data.Length - 10; i++)
            {
                if (data[i] == 122 && data[i + 1] == 0)
                {
                    ThingID = DataPackager.GetHostData64(Data, i - 8);

                }
            }
        }
    }

    public class ProcessTreasureData : Message
    {
        public long[] InstanceID { get; private set; }
        public uint[] TypeID { get; private set; }
        public Thing[] GetedItem { get; private set; }
        int i;
        public int ThingNum { get; private set; }
        public byte[] Versions { get; private set; }

        public ProcessTreasureData(byte[] data,Player player)
        {

            MessageType = MessageTypeEnum.Treasure;
            this.Data = data;
            var typeDatas = Data.Skip(10).Take(3).ToList();
            ThingNum = 0;

            InstanceID = new long[10];
            TypeID = new uint[10];
            GetedItem = new Thing[10];
            for (i = 0; i < Data.Length - 5; i++)
            {
                Versions = Data.Skip(i).Take(2).ToArray();
                if (Versions[0] == player.Version[0] && Versions[1] == player.Version[1])
                {
                    InstanceID[ThingNum] = DataPackager.GetHostData64(Data, i - 8);
                    typeDatas = Data.Skip(i + 2).Take(3).ToList();
                    typeDatas.Insert(0, 0);
                    TypeID[ThingNum] = (uint)DataPackager.GetHostData32(typeDatas.ToArray(), 0);
                    GetedItem[ThingNum] = Thing.GetThing(TypeID[ThingNum]);
                    if (GetedItem[ThingNum] == null)
                    {
                        GetedItem[ThingNum] = new Thing { Name = "未知物品", ID = (uint)TypeID[ThingNum] };

                    }
                    GetedItem[ThingNum].InstanceID = InstanceID[ThingNum];
                    ThingNum++;
                }
            }

        }
    }
   
}


